package com.fhao.trans.controller;

import com.fhao.trans.service.UserService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 声明式事务 @Transactional事务 自动开启事务,自动提交(程序运行成功)/回滚事务(程序运行失败)
 */
@RequestMapping("/trans")
@RestController
public class TransController {
    @Autowired
    private UserService userService;

    /**
     * 对异常进行捕获,事务会提交不会回滚.
     */
    @Transactional
    @RequestMapping("/register")
    public boolean register(String userName, String password) {
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表,result: " + result);
        //如果捕获异常的话方法感知不到运行失败, 事务也感知不到, 不会事务回滚
        try {
            int a = 10/0;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     *  捕获完异常,重新抛出去,事务会回滚.
     */
    @Transactional
    @RequestMapping("/r2")
    public boolean register2(String userName, String password) {
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表,result: " + result);
        try {
            int a = 10/0;
        }catch (Exception e) {
            throw e;
        }
        return true;
    }

    /**
     *  捕获完异常,手动回滚事务
     */
    @Transactional
    @RequestMapping("/r3")
    public boolean register3(String userName, String password) {
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表,result: " + result);

        try {
            int a = 10/0;
        }catch (Exception e) {
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return true;
    }

    /**
     * 捕获异常事务提交.
     */
    @Transactional
    @RequestMapping("/r4")
    public boolean register4(String userName, String password) throws IOException {
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表,result: " + result);

        if(true) {
            throw new IOException();
        }
        return true;
    }
    /**
     * 事务提交,讲道理抛出异常事务应该要回滚才对,但不知道为什么事务就是提交了.
     */
    @SneakyThrows
    @Transactional
    @RequestMapping("/r5")
    public boolean register5(String userName, String password) {
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表,result: " + result);
        if(true) {
            throw new IOException();
        }
        return true;
    }

    /**
     * 按照 r4 来看事务应该是会提交的  但是 r6 事务回滚了. 这是为什么??
     * 这是因为事务默认感知到 运行时异常才会回滚, 其他异常还是会正常提交
     */
    @Transactional
    @RequestMapping("/r6")
    public boolean register6(String userName, String password) throws IOException{
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表,result: " + result);
        if(true) {
            throw new RuntimeException();
        }
        return true;
    }
    /**
     * 捕获异常事务提交. 将r4的正常提交改成回滚
     */
    @Transactional(rollbackFor = Exception.class,isolation = Isolation.DEFAULT)
    //rollbackFor = Exception.class改成事务感知到所有异常都会回滚. isolation = Isolation.DEFAULT是在设置事务的隔离级别.
    @RequestMapping("/r7")
    public boolean register7(String userName, String password) throws IOException {
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表,result: " + result);
        if(true) {
            throw new IOException();
        }
        return true;
    }
}